<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of carts
 *
 * @author sonUIT
 */
class carts extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->library('cart');
    }

    public function load_page($namepage, $data) {
        if ($this->session->userdata('is_logged_in')) {
            $data['link'] = '';
            $data['link_logout'] = base_url() . 'index.php/welcome/dangxuat';
            $data['log_out'] = 'ĐĂNG XUẤT';
            $data['text_before'] = 'TÀI KHOẢN';
            $data['text_after'] = $this->session->userdata('email');
            $data['include'] = 'member.php';
            $this->load->view($namepage, $data);
        } else {
            $data['link'] = base_url() . 'index.php/welcome/dangky';
            $data['link_logout'] = '';
            $data['log_out'] = '';
            $data['text_before'] = 'ĐĂNG NHẬP';
            $data['text_after'] = 'ĐĂNG KÝ';
            $data['include'] = 'login.php';
            if ($namepage === 'trangchu' || $namepage === 'dangky') {
                $this->load->view($namepage, $data);
            }
            else
                $this->load->view('error', $data);
        }
    }

    public function index() {
        
        $this->load->model('sanpham');
        $data = array();
        $data['mota'] = array();
        $data['duongdan'] = array();
        $giohang = $this->cart->contents();
        foreach ($giohang as $row) {
            $sp = new Sanpham();
            $id = $row['id'];
            $sp->getObject("masp=$id");
            array_push($data['mota'], $sp->get_mota());
            array_push($data['duongdan'], $sp->get_duongdan());
        }
        
        $this->load_page('showcart',$data);
        //$this->load->view('showcart', $data);
    }

    public function add() {
        $id = trim($_GET['id']);
        $qty = trim($_GET['qty']);
        $price = trim($_GET['price']);
        $name = trim($_GET['name']);
        if ($this->check($id, $qty)) {
            $data2 = array(
                'id' => $id,
                'qty' => $qty,
                'price' => $price,
                'name' => $name
            );

            $this->cart->insert($data2);
        }
        $this->load->view('cart_homepage_view');
    }

    function check($id, $qty) {
        $data = $this->cart->contents();
        foreach ($this->cart->contents() as $item) {
            if ($item['id'] == $id) {
                // echo $item['id'] . $id;
                $num = intval($item['qty']) + intval($qty);
                $cartitem = array('rowid' => $item['rowid'], 'qty' => $num);
                $this->cart->update($cartitem);
                return false;
            }
        }
        return true;
    }

    public function update() {
        $rowid = trim($_GET['rowid']);
        $qty = trim($_GET['qty']);
        $dt = array('rowid' => $rowid, 'qty' => $qty);
        $this->cart->update($dt);
        $this->load->view('cart_homepage_view');
    }

    public function delete() {
        $rowid = trim($_GET['rowid']);
        $qty = 0;
        $dt = array('rowid' => $rowid, 'qty' => $qty);
        $this->cart->update($dt);
        $this->load->view('cart_homepage_view');
    }

    public function show() {
        $this->load->view('cart_homepage_view');
    }

    public function remove() {
        $this->cart->destroy();
    }

    public function address() {
          $this->load->model('KHACHHANG');
          
         $data = array();
         $data['phi']=  $this->input->get('phi');
        $email=  $this->session->userdata('email');
        $kh= new Khachhang();
       $temp= $kh->getListObject("*","email='$email'");
       $data['diachi']=$temp;
        $this->load_page('address', $data);
    }
	 public function printtopdf() {

        $this->load->library('Mpdf');
        $mpdf = new Pdf();
       // $html = "Vo Minh";
        $mpdf->SetAutoFont(AUTOFONT_ALL);
        $mpdf->SetHeader('{DATE j-m-Y}|AIOShop.vn|{PAGENO}');
        
        $dt=array();
        $dt['mpdf']=$mpdf;
        $this->load->view('in', $dt);
        
    }

}

?>